1169B - Pairs - CodeForces Solution


graphs implementation *1500

Please click on ads to support us..

Python Code:

import sys 
input = sys.stdin.readline
n, m = [int(i) for i in input().split()] 
p = [] 
l = []
ans = 'YES'
for i in range(m): 
    a, b =[ int(i) for i in input().split()] 
    if i==0 : l.append(a); l.append(b)
    if a not in l and b not in l : 
        if len(l) == 4 : ans = "NO"
        else : l.append(a); l.append(b)
    p.append((a, b)) 
if ans == "NO" : print(ans)
else : 
   
    res = "NO"
    if len(l) == 2 : print("YES") 
    else : 
                for i in range(4): 
            for j in range(i+1, 4): 
                x, y = l[i], l[j] 
                c = "YES"
                for k in range(m): 
                    if p[k][0] not in (x, y) and p[k][1] not in (x, y) : 
                        c = "NO"
                        break 
               
                if c=="YES" : res = "YES"; break 
            if res == "YES": break
            
        print(res)

C++ Code:

#include <iostream>
#include <algorithm>
#include <utility>
#include <cmath>
#include <stack>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define endl "\n"
#define pb(x) push_back(x)
#define all(v) (v).begin(), (v).end()
#define MAX  INT64_MAX
//============================================================================================================//
//-----------------------------------directions array---------------------------------------------------------//
int di[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
//============================================================================================================//
//-----------------------------------some constant--------------------------------------------------------------
const int SZ = 2 * (1e5 + 9);
const ll CON = (1e18);
const ll inf = (1 << 30);
const int MOD = (1e9 + 7);
const int M = (3005);
//============================================================================================================//
//-----------------------------------some functions--------------------------------------------------------------

void FAST() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
void Mostafa()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("errors.txt","w",stderr);
#endif
}
ll gcd(ll a, ll b)
{ return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b)
{ return a / gcd(a, b) * b; }

//============================================================================================================//
//---------------------------------------------------------------------------------------------------------------
bool so(int x,int n, vector<pair<int ,int>>v1,int v)
{
    int arr[v+10]{0};
    int bad =0,ma=0;
    for (int i = 0; i < n; ++i) {
       if  (x != v1[i].first && x != v1[i].second)
       {
            bad++;
            if (v1[i].first != v1[i].second)
            {
                arr[v1[i].first]++;
                arr[v1[i].second]++;
            }
            else arr[v1[i].first]++;
            ma = max ({arr[v1[i].first],ma,arr[v1[i].second]});
       }
    }
   // cout <<bad <<" "<<ma<<endl;
    return (bad==ma);

}

void solve() {
   int n,v;
   cin >>v>>n;
    vector<pair<int ,int>>v1(n);
    for (int i = 0; i < n; ++i) {
        cin >> v1[i].first>>v1[i].second;
    }
    if (so(v1[0].first,n,v1,v) ||so(v1[0].second,n,v1,v) )
        cout <<"YES";
    else cout <<"NO";



}
int main ()
{
    FAST();
    Mostafa();
    int q = 1;
  // cin >>q;
    while(q--)
    {
        solve();
    }
}
 		    	 	  		   		 	 	 	  			


Comments

Submit
0 Comments
More Questions

279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters